package boardgamer.yavalath.client.game;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.RectF;
import boardgamer.common.board.TiledBoard.TileFactory;
import boardgamer.common.client.layout.HasLayout;
import boardgamer.common.net.PlayerId;
import boardgamer.yavalath.client.RenderHelper;
import boardgamer.yavalath.client.resources.ImageHandler;
import boardgamer.yavalath.common.YavalathPlayerState;
import boardgamer.yavalath.common.YavalathTile;

import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;


public class YavalathDrawableTile extends YavalathTile implements HasLayout {

  private static final long serialVersionUID = -3147131649178360041L;

  public static final int DEFAULT_COLOR = Color.rgb(192, 128, 192);

  private final Map<PlayerId, YavalathPlayerState> playersMap;
  private final Paint paint;
  private RectF boundingRect;

  private YavalathDrawableTile(YavalathTile tile, Map<PlayerId, YavalathPlayerState> playersMap,
      int alpha) {
    super(tile.getX(), tile.getY());
    if (tile.hasOwner()) {
      this.setOwner(tile.getOwner());
    }

    this.playersMap = playersMap;
    this.paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    this.paint.setColorFilter(new LightingColorFilter(DEFAULT_COLOR, 0));
    this.paint.setAlpha(alpha);
    this.boundingRect = new RectF(0, 0, ImageHandler.tile.getWidth(),
        ImageHandler.tile.getHeight());
  }

  public void setAlpha(int alpha) {
    paint.setAlpha(alpha);
  }

  public int getAlpha() {
    return paint.getAlpha();
  }

  public void render(Canvas canvas) {
    canvas.drawBitmap(ImageHandler.tile, null /* srcRect */, boundingRect, paint);
  }

  @Override
  public void setOwner(PlayerId playerId) {
    super.setOwner(playerId);
    paint.setColorFilter(
        new LightingColorFilter(
            RenderHelper.instance().getColor(playersMap.get(playerId).getColor()), 0));
  }

  @Override
  public void setBoundingRect(RectF boundingRect) {
    this.boundingRect = checkNotNull(boundingRect, "boundingRect");
  }

  @Override
  public RectF getBoundingRect() {
    return new RectF(boundingRect);
  }

  public void unsetOwner() {
    this.owner = null;
    paint.setColorFilter(new LightingColorFilter(DEFAULT_COLOR, 0));
  }

  public static class Factory implements TileFactory<YavalathDrawableTile> {
    private static final long serialVersionUID = -8485759472858804197L;

    private final Map<PlayerId, YavalathPlayerState> playersMap;
    private final int startAlpha;

    public Factory(Map<PlayerId, YavalathPlayerState> playersMap, int startAlpha) {
      this.playersMap = checkNotNull(playersMap, "playersMap");
      this.startAlpha = startAlpha;
    }

    public YavalathDrawableTile create(YavalathTile tile, int alpha) {
      return new YavalathDrawableTile(tile, playersMap, alpha);
    }

    @Override
    public YavalathDrawableTile createTile(int x, int y) {
      return create(new YavalathTile(x, y), startAlpha);
    }

    @Override
    public YavalathDrawableTile copyTile(YavalathDrawableTile tile) {
      throw new UnsupportedOperationException("Cannot copy Yavalath Drawable Tile");
    }
  }
}
